//
//  BKUserFindPwdViewController.m
//  bestkeep
//
//  Created by windCould on 16/11/29.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKUserFindPwdViewController.h"
#import "BKMyCommon.h"
#import "BKCustextFieldView.h"
#import "BKUserFindPwdNextViewController.h"
#import "BKUserUnitRequestAPI.h"
#import "BKUserCodeFormat.h"
#import "BKUserRequestResultModel.h"
@interface BKUserFindPwdViewController ()<BKCustextFieldViewDelegate,UITextFieldDelegate>

@property (nonatomic,strong)BKCustextFieldView  * accountTexField;
@property (nonatomic,strong)BKCustextFieldView  * imgCodeTexField;
@property (nonatomic,strong)UIButton * nextBtn;
@property (nonatomic,copy)NSString * imgCodeKey;


@end


@implementation BKUserFindPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"找回密码";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    self.imgCodeKey = @"";
    [self setUpViews];
    [self getFindPwdImgCode];
    
}

#pragma mark =========================NetRequest=======================
-(void)getFindPwdImgCode
{
    [BKUserUnitRequestAPI getUserImgCodeWithBlock:^(id obj, NSError *error) {
        
        NSDictionary * dataDict = obj;
        if (dataDict[@"data"]) {
            
            self.imgCodeTexField.rightImg = [UIImage imageWithData:dataDict[@"data"]];
            self.imgCodeKey = dataDict[@"imgCodeKey"];
            NSLog(@"请求图片验证码成功。。。。");
            
        } else {
            
            self.imgCodeTexField.rightImg = [UIImage imageNamed:@"BK_My_button_ refurbish_"];
            NSLog(@"请求图片验证码失败。。。。");
        }
        
    }];
    
}

#pragma mark =========================BKCustextFieldViewDelegate========
-(void)custextFieldView:(BKCustextFieldView *)custextFieldView imgCodeButtonTouch:(UIButton *)btn
{
    [self getFindPwdImgCode];
}

#pragma mark =========================Function=========================

-(void)goToNextBtnClick
{

    BOOL result = [self isRightFormatWithAccount:self.accountTexField.textField.text imgCodeStr:self.imgCodeTexField.textField.text];
    if (result) {
        
        [BKIndicationView showInView:self.view];
        [BKUserUnitRequestAPI findPwdFirstStepWithAccount:self.accountTexField.textField.text imgCodeStr:self.imgCodeTexField.textField.text imgCodeKey:self.imgCodeKey resultBlock:^(id obj, NSError *error) {
            
            [BKIndicationView dismiss];
            if (obj) {
                
                BKUserRequestResultModel * result = obj;
                if ([result.success isEqualToString:@"1"]) {
                    
                    
                    [HUD showSuccessMessage:result.msg];
                    BKUserFindPwdNextViewController * nextVC = [[BKUserFindPwdNextViewController alloc]init];
                    nextVC.accountStr = self.accountTexField.textField.text;
                    nextVC.imgCodeStr = self.imgCodeTexField.textField.text;
                    [self.navigationController pushViewController:nextVC animated:YES];
                } else {
                
                    [HUD showErrorMessage:result.msg];
                }
            } else {
                
                NSLog(@"%@",error.localizedDescription);
                if (error.code==502) {
                    [HUD showErrorMessage:@"服务器开小差了，请稍后再试！"];
                }else{
                    [HUD showErrorMessage:error.localizedDescription];
                }
            }
        }];
    }
    
  
    
}

-(BOOL)isRightFormatWithAccount:(NSString *)account imgCodeStr:(NSString *)codeStr
{
    if (![BKUserCodeFormat parameterValueIsNotNilAndZeroLengthWithContentStr:account]) {
        
        NSLog(@"请输入手机号。。。。");
        [HUD showErrorMessage:@"请输入手机号码"];
        return NO;
    }
    
    if (![BKUserCodeFormat userMobileCodeOfBKPlatformatIsAdaptRequireWidthMobileNum:account]) {
        
        NSLog(@"请输入正确的手机号");
        [HUD showErrorMessage:@"请输入正确的手机号"];
        return  NO;
    }
    
    if (![BKUserCodeFormat parameterValueIsNotNilAndZeroLengthWithContentStr:codeStr]) {
        
        NSLog(@"请输入图形验证码。。");
        [HUD showErrorMessage:@"请输入图形验证码"];
        return  NO;
    }
    

    return YES;
}


#pragma mark =========================布局==============================

-(void)setUpViews
{
    _accountTexField = [[BKCustextFieldView alloc]initWithFrame:CGRectZero style:BKCustextFieldViewDefultType];
    _accountTexField.placeHolderString = @"请输入手机号码";
    _accountTexField.textField.delegate = self;
    _accountTexField.textField.keyboardType = UIKeyboardTypeNumberPad;
    _accountTexField.imgUrlStr = @"BK_My_butter_icon_02";
    [self.view  addSubview:_accountTexField];
    
    _imgCodeTexField = [[BKCustextFieldView alloc]initWithFrame:CGRectZero style:BKCustextFieldViewImageCodeType];
    _imgCodeTexField.myDlegate = self;
    _imgCodeTexField.textField.delegate = self;
    _imgCodeTexField.placeHolderString = @"请输入图形验证码";
    _imgCodeTexField.imgUrlStr = @"BK_My_imgcode";
    [self.view addSubview:_imgCodeTexField];
    
    _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _nextBtn.titleLabel.font = UIFontBoldCustom(18);
    [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    _nextBtn.layer.cornerRadius = 4;
    _nextBtn.layer.masksToBounds = YES;
    [_nextBtn addTarget:self action:@selector(goToNextBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _nextBtn.backgroundColor = BK_MY_COLOR_01;
    [self.view addSubview:_nextBtn];
    
    [self layoutviews];
    
}


-(void)layoutviews
{
    
    [_accountTexField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.height.mas_equalTo(@(50));
    }];
    
    
    [_imgCodeTexField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_accountTexField.mas_bottom).offset(0);
        make.left.mas_equalTo(_accountTexField.mas_left);
        make.right.mas_equalTo(_accountTexField.mas_right);
        make.height.mas_equalTo(@(50));
    }];
    
    
    [_nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_imgCodeTexField.mas_bottom).offset(20);
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.height.mas_equalTo(@(45));
        
    }];
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}



@end
